#--------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------- Portmann & Stojanovic ----------------------------------------------#
#--------------------------------------------------- November 2020 --------------------------------------------------#
#------- Are Immigrant-Origin Candidates Penalized in Virtue of Ingroup Favoritism or Outrgoup Hostility? -----------#
#--------------------------------------------------------------------------------------------------------------------#
#-------------------------------------------- D3 Furher Robustness Checks -------------------------------------------#
#--------------------------------------------------------------------------------------------------------------------#

lm(list=ls())
setwd(".../...")
load("d_nr15_cand")
load("d_nr15_cand_m")
load("d_nr15_cand_p")
load("d_nr15_cand_n") 

# ------------------------------------ Table 18: Positive preference votes vs. non-Swiss name, variation by district magnitude

# Negative preference votes

m1 <- lme(npv_rel ~  nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + nonswissname*distmag_c, random = ~ 1 | list_id, data=d_nr15_cand_n)

# Positive preference votes

d_nr15_cand_p <- within(d_nr15_cand_p, partypos <- relevel(partypos, ref = 1))

m3 <- glmer(pos_mean ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + nonswissname*distmag_c + (1|list_id), data=d_nr15_cand_p, family=inverse.gaussian(link="log"))

# Table

custom.name <- c("Intercept", "Non-Swiss name", 
                 "Relative position on ballot", "Incumbent",
                 "Pre-cumulated", "Male", "Age: 30-50 years ", "Age: 50+ years", 
                 "Party position = centre", "Party position = left",
                 "District magnitude",
                 "Non-Swiss name x district magnitude")

texreg(list(m1, m3), booktabs=TRUE,
       caption="Positive preference votes vs. non-Swiss name",
       #omit.coef = "bfscantno",
       custom.coef.names = custom.name,
       #reorder.coef = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1),
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

# ------------------------------------ Figure 5: Predicted negative preference votes and positive preference votes, by candidate name and
#district magnitude

m1 <- lme(npv_rel ~  nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + nonswissname*distmag, random = ~ 1 | list_id, data=d_nr15_cand_n)
m3 <- glmer(pos_mean ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + nonswissname*distmag + (1|list_id), data=d_nr15_cand_p, family=inverse.gaussian(link="log"), nAGQ=0)


colors <- c("black", "grey")

ef <- effect("nonswissname:distmag", m1)
summary(ef)
ef <- as.data.frame(ef)
f_negint <- ggplot(ef, aes(distmag, fit, color=nonswissname, fill=nonswissname)) + 
  geom_line(size=1) + 
  geom_ribbon(aes(ymin=lower, ymax=upper), size=0.1, alpha=0.5) + 
  theme_bw() + labs(y ="Predicted negative preference votes", x= "District magnitude", color="Candidate name") + 
  scale_colour_manual(values = colors, aesthetics = c("colour", "fill")) + guides(fill=FALSE); f_negint


ef2 <- effect("nonswissname:distmag", m3)
summary(ef)
ef2 <- as.data.frame(ef2)
f_posint <- ggplot(ef2, aes(distmag, fit, color=nonswissname, fill=nonswissname)) + 
  geom_line(size=1) + 
  geom_ribbon(aes(ymin=lower, ymax=upper), size=0.1, alpha=0.5) + 
  theme_bw() + labs(y ="Predicted positive preference votes", x= "District magnitude", color="Candidate name") + 
  scale_colour_manual(values = colors, aesthetics = c("colour", "fill")) + guides(fill=FALSE); f_posint

pred_dist <- ggarrange(f_negint, f_posint, 
                       labels = c("", ""),
                       ncol = 2, nrow = 1)
pred_dist

png("pred_dist.png", width = 8.5, height = 4.5, units = 'in', res = 300)
plot(pred_dist)
dev.off()

#------------------------------- Table 19 Appendix: Effect of candidate names (Swiss, non-Swiss) on negative and positive preference votes,
# with profession

d_nr15_cand_n_prof <- d_nr15_cand_n[d_nr15_cand_n$prof_c3_b!=0, ]
d_nr15_cand_p_prof <- d_nr15_cand_p[d_nr15_cand_p$prof_c3_b!=0, ]

# ----- Models

m1 <- lme(npv_rel ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + prof_c3_b + partypos + num_listpos_c + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n_prof)

m2 <- glmer(pos_mean ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + prof_c3_b + partypos + num_listpos_c + bfscantno.x + (1|list_id), data=d_nr15_cand_p_prof, nAGQ=0, family=inverse.gaussian(link="log"))

# ----- Tables

custom.name <- c("Intercept", "Non-Swiss name (0/1)", 
                 "Relative position on ballot", "Incumbent (0/1)",
                 "Pre-cumulated (0/1)", "Male (0/1)", "Age: 30-50 years ", "Age: 50+ years", 
                 "Profession = medium-skilled", "Profession = low-skilled",
                 "Party position = center", "Party position = left",
                 "Number of candidates on ballot")

texreg(list(m1, m2), booktabs=TRUE,
       caption="Negative preference votes vs. non-Swiss name, with profession",
       omit.coef = "bfscantno",
       custom.coef.names = custom.name,
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

#-------------------------------- Table 20 Appendix: Origin of candidate names, by party

d_nr15_cand_m$origin_name_base <- factor(d_nr15_cand_m$origin_name_base, 
                                         levels = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 99), 
                                         labels = c("Swiss", "Hispanic", "Eastern European", "Southern European",
                                                    "Yugoslavian", "Albanian", "Western European/Nordic/Anglo", "Indian", "Eastern Asian",
                                                    "Arabic", "Central Asian", "Turkish/Kurdish", "(Other) African", "Unknown"))

d_nr15_cand_m$partypos <- factor(d_nr15_cand_m$partypos, 
                                 levels = 1:3, 
                                 labels = c("Right", "Center", "Left"))

AT3 <- d_nr15_cand_m %>% dplyr::ungroup() %>%
  dplyr::select(origin_name_base, partypos) %>% 
  dplyr::group_by(partypos, origin_name_base) %>%
  dplyr::summarise (n=n()) %>%
  dplyr::mutate(freq = (n / sum(n))*100)

AT3$freq <- round(AT3$freq, digits = 1)

AT3_n <- dcast(AT3, origin_name_base ~ partypos, value.var=c("n"))
print(xtable(AT3_n), include.rownames=FALSE)

AT3_freq <- dcast(AT3, origin_name_base ~ partypos, value.var=c("freq"))
print(xtable(AT3_freq), include.rownames=FALSE)


#-------------------------------- Table 21 Appendix: Effect of candidate names (Swiss vs. non-Swiss) on ballot position

d_nr15_cand_m_prof <- d_nr15_cand_m[d_nr15_cand_m$prof_c3_b!=0, ]

m1 <- lme(rel_pos ~ nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_m_prof)
summary(m1)

m2 <- lme(rel_pos ~ nonswissname + bfsincumbency + precumulated + bfssex + age_c3 + prof_c3_b + partypos + num_listpos_c + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_m_prof)

m3 <- lme(rel_pos ~ nonswissname + partypos + bfsincumbency + precumulated + bfssex + age_c3 + prof_c3_b + partypos + num_listpos_c + partypos*nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_m_prof)

# ----- Table

custom.name <- c("Intercept", "Non-Swiss name (0/1)", 
                 "Incumbent (0/1)",
                 "Pre-cumulated (0/1)", "Male (0/1)", "Age: 30-50 years ", "Age: 50+ years", 
                 "Profession = medium-skilled", "Profession = low-skilled",
                 "Party position = center", "Party position = left",
                 "Number of candidates on ballot",
                 "Non-Swiss name*Party position = center", 
                 "Non-Swiss name*Party position = left")

texreg(list(m1, m2, m3), booktabs=TRUE,
       caption="Effect of candidate names (Swiss vs. non-Swiss) on ballot position",
       omit.coef = "bfscantno",
       custom.coef.names = custom.name,
       #reorder.coef = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1),
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

#-------------------------------- Table 22: Effect of candidate names on negative preference votes, considering proportion of candidates
#with non-Swiss names on party list

# ----- Build variable proportion of candidates with non-Swiss names on list

#Negative preference votes


table(d_nr15_cand_n$nonswissname)
d_nr15_cand_n$nonswissname_num <- ifelse(d_nr15_cand_n$nonswissname == "Swiss name", 0, 1)

  d_nr15_cand_n <- d_nr15_cand_n %>% 
  dplyr::group_by(list_id) %>% 
  dplyr::mutate(num_unique_cand = n_distinct(cand_id), 
                num_unique_nonswiss = sum(nonswissname_num),
                prop_nonswiss = num_unique_nonswiss / num_unique_cand)

d_nr15_cand_n$prop_nonswiss_c <- scale(d_nr15_cand_n$prop_nonswiss, center=TRUE, scale=FALSE)

#Positive preference votes

table(d_nr15_cand_m$nonswissname)
d_nr15_cand_m$nonswissname_num <- ifelse(d_nr15_cand_m$nonswissname == "Swiss name", 0, 1)

d_nr15_cand_m <- d_nr15_cand_m %>% 
  dplyr::group_by(list_id) %>% 
  dplyr::mutate(num_unique_cand = n_distinct(cand_id), 
                num_unique_nonswiss = sum(nonswissname_num),
                prop_nonswiss = num_unique_nonswiss / num_unique_cand)

d_nr15_cand_m$prop_nonswiss_c <- scale(d_nr15_cand_m$prop_nonswiss, center=TRUE, scale=FALSE)

# ------ Models negative pereference votes

m34 <- lme(npv_rel ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos + num_listpos_c + prop_nonswiss_c + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)

m35 <- lme(npv_rel ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos +  partypos + num_listpos_c + prop_nonswiss_c*nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)

m36 <- lme(npv_rel ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos + num_listpos_c +  partypos*nonswissname + prop_nonswiss_c*nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)

# ------ Table

custom.name <- c("Intercept", "Non-Swiss name", 
                 "Relative position on ballot", "Incumbent",
                 "Pre-cumulated", "Male", "Age: 30-50 years ", "Age: 50+ years", 
                 "Party position = centre", "Party position = left",
                 "Number of candidates on ballot", "Proportion non-Swiss names party list",
                 "Non-Swiss name*Proportion non-Swiss (centered)", 
                 "Non-Swiss name*Party position = center", 
                 "Non-Swiss name*Party position = left")

texreg(list(m34, m35, m36), booktabs=TRUE,
       caption="Effect of candidate names on negative preference votes, with proportion of non-Swiss candidates on party list",
       omit.coef = "bfscantno",
       custom.coef.names = custom.name,
       #reorder.coef = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1),
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

# ------ Figure negative preference votes

m37 <- lme(npv_rel ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos + num_listpos_c +  prop_nonswiss*nonswissname + partypos*nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)

ef <- effect("prop_nonswiss:nonswissname", m37)
summary(ef)
ef <- as.data.frame(ef)

colors <- c("black", "grey")
f_negint_propnonswiss <- ggplot(ef, aes(prop_nonswiss, fit, color=nonswissname, fill=nonswissname)) + 
  geom_line(size=1) + 
  geom_ribbon(aes(ymin=lower, ymax=upper), size=0.1, alpha=0.5) + 
  theme_bw() + labs(y ="Predicted negative preference votes", x= "Share non-Swiss names on party list", color="Candidate name") + 
  scale_colour_manual(values = colors, aesthetics = c("colour", "fill")) + guides(fill=FALSE); f_negint_propnonswiss

#-------------------------------- Table 23 Appendix: Effect of candidate names on positive preference votes, considering proportion of candidates
#with non-Swiss names on party list

# ------ Models positive preference votes

d_nr15_cand_m$partypos <- factor(d_nr15_cand_m$partypos)

m38 <- lme(pos_mean ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + num_listpos_c + prop_nonswiss_c + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_m)

m39 <- lme(pos_mean ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos + num_listpos_c + bfscantno.x + prop_nonswiss_c*nonswissname, random = ~ 1 | list_id, data=d_nr15_cand_m)

m40 <- lme(pos_mean ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos + num_listpos_c  + bfscantno.x + prop_nonswiss_c*nonswissname + partypos*nonswissname, random = ~ 1 | list_id, data=d_nr15_cand_m)

# ------ Table

texreg(list(m38, m39, m40), booktabs=TRUE,
       caption="Effect of candidate names on positive preference votes, with proportion of non-Swiss candidates on party list",
       omit.coef = "bfscantno",
       custom.coef.names = custom.name,
       #reorder.coef = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1),
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

# ------ Figure posiive preference votes

m41 <- lme(pos_mean ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
             partypos + num_listpos_c +  prop_nonswiss*nonswissname + partypos*nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)
summary(m41)

ef <- effect("prop_nonswiss:nonswissname", m41)
summary(ef)
ef <- as.data.frame(ef)

colors <- c("black", "grey")
f_posint_propnonswiss <- ggplot(ef, aes(prop_nonswiss, fit, color=nonswissname, fill=nonswissname)) + 
  geom_line(size=1) + 
  geom_ribbon(aes(ymin=lower, ymax=upper), size=0.1, alpha=0.5) + 
  theme_bw() + labs(y ="Predicted positive preference votes", x= "Share non-Swiss names on party list", color="Candidate name") + 
  scale_colour_manual(values = colors, aesthetics = c("colour", "fill")) +  
  guides(fill=FALSE); f_posint_propnonswiss


# -------------------------- Figure 6 Appendix: Predicted negative and positive preference votes, by candidate name and share of candidates
#with non-Swiss names on the party list

int_propnonswiss <- ggarrange(f_negint_propnonswiss, f_posint_propnonswiss, 
                              labels = c("", ""),
                              ncol = 2, nrow = 1)
int_propnonswiss

png("Figures/int_propnonswiss.png", width = 8.5, height = 4, units = 'in', res = 300)
plot(int_propnonswiss)
dev.off()


#------------------------------- Table 24 Appendix: Effect of candidate names (Swiss vs. non-Swiss) on negative and positive preference votes,
#only lists gaining at least one seat in parliament

# ----- Generate variable at least one candidate of list elected

d_nr15_cand_n <- d_nr15_cand_n %>% 
  dplyr::group_by(list_id) %>%
  dplyr::mutate(elect_list = sum(bfselected))

d_nr15_cand_p <- d_nr15_cand_p %>% 
  dplyr::group_by(list_id) %>%
  dplyr::mutate(elect_list = sum(bfselected))

# ----- Generate data

d_nr15_cand_p_elect <- d_nr15_cand_p[d_nr15_cand_p$elect_list > 0, ]
d_nr15_cand_n_elect <- d_nr15_cand_n[d_nr15_cand_n$elect_list > 0, ]

# ----- Estimate model negative preference votes

m32 <- lme(npv_rel ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
             partypos + num_listpos_c + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n_elect)
summary(m32)

# ----- Estimate model positive preference votes

m33 <- glmer(pos_mean ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos + num_listpos_c + bfscantno.x + (1|list_id), d_nr15_cand_p_elect, family=inverse.gaussian(link="log"))
summary(m33)

custom.name <- c("Intercept", "Non-Swiss name (0/1)", 
                 "Relative position on ballot", "Incumbent (0/1)",
                 "Pre-cumulated (0/1)", "Male (0/1)", "Age: 30-50 years ", "Age: 50+ years", 
                 "Party position = center", "Party position = left",
                 "Number of candidates on ballot")

texreg(list(m32, m33), booktabs=TRUE,
       caption="Positive preference votes vs. non-Swiss name, only lists winning seat in parliament",
       omit.coef = c("bfscantno"),
       custom.coef.names = custom.name,
       #reorder.coef = c(2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 3, 4),
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

